package objects;


import java.awt.Graphics;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

import audioPlayer.AudioPlayer;
import com.google.gson.annotations.Expose;
import communication.ClipRecord;
import drawables.Drawable;
import drawables.DrawableManager;
import drawables.Type;
import java.io.Serializable;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Reflection;
import javafx.scene.image.ImageView;

public abstract class AudioClip extends Drawable implements Serializable {
	
        protected String id;
        
	protected Track currentTrack=null;
        
	protected boolean ready=false;
	protected int xOffset;
	protected int yOffset;
	protected boolean inHand=false;
	protected long startTime=0;
	protected String title;

	protected String audioUrl;
	protected String uplaoder;
	protected String instrument;
        protected String genre;
        protected int bpm;
        protected String key;
        protected float rating;
        transient protected ImageView loadingImage=null;

    public void setLoadingImage(ImageView loadingImage) {
        this.loadingImage = loadingImage;
    }

        
        
    public String getTitle() {
        return title;
    }

    public String getUplaoder() {
        return uplaoder;
    }

    public String getGenre() {
        return genre;
    }

    public String getInstrument() {
        return instrument;
    }

    public float getRating() {
        return rating;
    }

    public int getBpm() {
        return bpm;
    }

    public String getKey() {
        return key;
    }
        
    
        
	public abstract AudioClip clone();
	
	
	public Track getCurrentTrack() {
		return currentTrack;
	}
	
	public void setCurrentTrack(Track currentTrack) {
		this.currentTrack = currentTrack;
	}
	
	public long getStartTime(){
		return startTime;
	}
	
	public void setStartTime(long startTime){
		this.startTime = startTime;
	}
	
	
	public abstract void loadAudio(final String url);
        public void loadAudio(){
            loadAudio(audioUrl);
        }
	
	public abstract void play();
	
        public abstract void previewPlay();
        
	public abstract void stop();
	
	public abstract double getDuration();
	
//	public AudioClip(int x , int y){
//		super(x , y);	
//		height=20;
//		width=20;
//	}
	
	public AudioClip(String audioUrl, String id ,  String title, String uplaoder, String instrument , String genre , int bpm , String key , float rating){
		height=20;
		width=20;
                this.id = id;
		this.title = title;
		this.audioUrl = audioUrl.replace(" ", "%20");
                
                
                                
                        
                System.out.println("------URL " + this.audioUrl);
                this.uplaoder = uplaoder;
                this.instrument = instrument;
                this.genre = genre;
                this.rating = rating;
                
                this.bpm = bpm;
                this.key = key;
		//loadAudio(audioUrl);
	}
	
	
	
	@Override
	public void paint(Graphics g) {	
		if(ready){
			
			width = AudioPlayer.get().timeToMesurment(getDuration());
			
			
			//g.setColor(Color.green);
			g.fillRoundRect(x , y , width, height, 5, 5);
			//g.setColor(Color.black);
			g.drawRoundRect(x , y , width, height, 5, 5);
			g.drawString(title, x+3, y+15);
		}
	}
	
	@Override
	public void paint(GraphicsContext g) {
		if(ready){
			
                        //g.applyEffect(new Reflection(0, 0, 0, 0));
			width = AudioPlayer.get().timeToMesurment(getDuration());
			
			g.setFill(Color.web("#8fff00"));
                        g.setStroke(Color.web("#080066"));
			g.fillRoundRect(x , y , width, height, 5, 5);
			//g.setColor(Color.black);
                        if(selected){
                            //g.setStroke(Color.web("#000E96"));
                            g.setLineWidth(2);
                        }
                        else{
                            //g.setStroke(Color.web("#080066"));
                            g.setLineWidth(0.7);
                        }
                        g.strokeRoundRect(x , y , width, height, 5, 5);
                        g.setStroke(Color.web("#000000"));
                        g.setFill(Color.web("#000000"));
                        g.setLineWidth(0.7);
			g.strokeText(title, x+3, y+15);
		}
		
	}
	
        private void putInFront(){
            DrawableManager.get().remove(this);
            DrawableManager.get().add(this);
        }
	
	@Override
	public void onRightClick(int x , int y){
		AudioClip cloned = clone();
		cloned.setX(x);
		cloned.setY(y);
		currentTrack.addAudioClip(cloned);
                
	}
	
	@Override
	public void onLeftClick(int x , int y){
		xOffset = x - this.x;
		yOffset = y - this.y;
		inHand = true;
		currentTrack.removeAudioClip(this);
		currentTrack = null;
		//DrawableManager.get().remove(this);
                putInFront();
	}

	@Override
	public void onDrag(int x , int y){
		this.x = x - xOffset;
		this.y = y - yOffset;
	}
	
	@Override
	public void onDrop(Drawable d){
		inHand = false;
		//DrawableManager.get().add(this);
	}
	@Override
	public void onDelete(){
            currentTrack.removeAudioClip(this);
            DrawableManager.get().remove(this);
            
        }
          
	@Override
	public boolean contains(int x , int y){
		if(inHand)
			return false;
		else
			return super.contains(x, y);
	}

    public boolean isReady() {
        return ready;
    }
      
    @Override
    public Type getType(){
        return Type.CLIP;
    }
        
    
    public abstract void setVolume(double volume);
    public abstract void setPan(double balance);
    
    ClipRecord getClipRecord(){
        ClipRecord cr = new ClipRecord();
        
        cr.setInfo(title);
        cr.setGenre(genre);
        cr.setInstrument(instrument);
        cr.setStartTime(startTime);
        cr.setUploader(uplaoder);
        cr.setUrl(audioUrl);
        cr.setId(id);
        return cr;
    }
}
